package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureStandardJobVo;
import org.dromara.agriculture.domain.bo.AgricultureStandardJobBo;
import org.dromara.agriculture.service.IAgricultureStandardJobService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 标准作业任务
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/standardJob")
public class AgricultureStandardJobController extends BaseController {

    private final IAgricultureStandardJobService agricultureStandardJobService;

    /**
     * 查询标准作业任务列表
     */
    @SaCheckPermission("agriculture:standardJob:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureStandardJobVo> list(AgricultureStandardJobBo bo, PageQuery pageQuery) {
        return agricultureStandardJobService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出标准作业任务列表
     */
    @SaCheckPermission("agriculture:standardJob:export")
    @Log(title = "标准作业任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureStandardJobBo bo, HttpServletResponse response) {
        List<AgricultureStandardJobVo> list = agricultureStandardJobService.queryList(bo);
        ExcelUtil.exportExcel(list, "标准作业任务", AgricultureStandardJobVo.class, response);
    }

    /**
     * 获取标准作业任务详细信息
     *
     * @param jobId 主键
     */
    @SaCheckPermission("agriculture:standardJob:query")
    @GetMapping("/{jobId}")
    public R<AgricultureStandardJobVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long jobId) {
        return R.ok(agricultureStandardJobService.queryById(jobId));
    }

    /**
     * 新增标准作业任务
     */
    @SaCheckPermission("agriculture:standardJob:add")
    @Log(title = "标准作业任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureStandardJobBo bo) {
        return toAjax(agricultureStandardJobService.insertByBo(bo));
    }

    /**
     * 修改标准作业任务
     */
    @SaCheckPermission("agriculture:standardJob:edit")
    @Log(title = "标准作业任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureStandardJobBo bo) {
        return toAjax(agricultureStandardJobService.updateByBo(bo));
    }

    /**
     * 删除标准作业任务
     *
     * @param jobIds 主键串
     */
    @SaCheckPermission("agriculture:standardJob:remove")
    @Log(title = "标准作业任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] jobIds) {
        return toAjax(agricultureStandardJobService.deleteWithValidByIds(List.of(jobIds), true));
    }
}
